var orgall = null;//所有人员
var userall = null;//所有单位
var operation_state = null;//项目状态数据
var category_type = null;//项目业务类型数据
var progect_type = null;
$(function () {
    $.when( vm.init()
        ).done(
        $("#jqGrid").jqGrid({
            url: baseURL + 'bss/apbase/shgdxmlist',
            datatype: "json",
            colModel: [
                {label: 'guid', name: 'guid', index: 'guid', width: 50, key: true, hidden: true},
               {label: '项目名称', name: 'pBName', index: 'p_b_name', width: 200},
               {
                label: '项目单位', name: 'pBOrgid', index: 'p_b_orgid', width: 80,
                    formatter: function (cellvalue, options, rowObject) {

                        for (d in orgall) {
                            if (cellvalue == orgall[d].deptId)
                                return orgall[d].name;
                        }
                    }
                },


                {
                    label: '参与人', name: 'pBCollectid', index: 'p_b_collectid', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        var names = "";


                        var ss = (cellvalue.replace(/\"/g, "").replace("[", "").replace("]", "") + "").split(",");
                        for (var i = 0; i < ss.length; i++) {

                            for (d in userall) {
                                if (ss[i] == userall[d].userId + "") {

                                    names += userall[d].username + ",";
                                }
                            }
                        }
                        if (names.length > 0) {
                            names = names.substring(0, names.length - 1);
                        }
                        return names;
                    }
                },
                {
                    label: '业务类型', name: 'pBCategory', index: 'p_b_category', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in category_type) {
                            if (cellvalue == category_type[d].dKey) return category_type[d].dValue;
                        }
                    }
                },

                {
                    label: '项目状态', name: 'pOperationState', index: 'p_operation_state', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in operation_state) {
                            if (cellvalue == operation_state[d].dKey) return operation_state[d].dValue;
                        }
                    }
                },



                {label: '项目地址', name: 'pBAddress', index: 'p_b_address', width: 100},
                {
                    label: '创建时间', name: 'pBAddtime', index: 'p_b_addtime', width: 80,
                    formatter: function (value, options, row) {
                        return value != null ? new Date(value).Format('yyyy-MM-dd hh:mm') : "";
                    }
                },
                {
                    label: '操作', name: '', width:  240,
                    formatter: function (cellvalue, options, rowObject) {
                        return  "<a  class='btn btn-primary' onclick='showxmgk(\"" + rowObject.guid + "\")' >&nbsp;项目概况查看</a>&nbsp;"+
                            "<a  class='btn btn-primary' onclick='showcj(\"" + rowObject.guid + "\",\"" + rowObject.pBLongitude + "\" ,\"" + rowObject.pBLatitude + "\")' >&nbsp;现场信息查看</a>&nbsp;&nbsp;<br>" +
                            "<a  class='btn btn-primary' onclick='showgzb(\"" + rowObject.guid + "\" )' >&nbsp;购置表</a>&nbsp;&nbsp;"+
                            "<a  class='btn btn-primary' onclick='showccb(\"" + rowObject.guid + "\")' >&nbsp;拆除表</a>&nbsp;&nbsp;"+
                            "<a  class='btn btn-primary' onclick='showDXF(\"" + rowObject.guid + "\",\"" + rowObject.pBName + "\")' >&nbsp;&nbsp;CAD走向图&nbsp;&nbsp;</a>&nbsp;<br>"+
                            "<a  class='btn btn-primary' onclick='showsqs(\"" + rowObject.guid + "\" )' >&nbsp;申请书查看</a>&nbsp;&nbsp;" +
                            "<a  class='btn btn-primary' onclick='showfjdc(\"" + rowObject.guid + "\" )' >&nbsp;导出结果</a>&nbsp;&nbsp;";
                        }
                }


            ],
            viewrecords: true,
            height: 370,
            rowNum: 4,
            rowList: [10, 30, 50],
            rownumbers: true,
            rownumWidth: 25,
            autowidth: true,
            multiselect: true,
            pager: "#jqGridPager",
            jsonReader: {
                root: "page.list",
                page: "page.currPage",
                total: "page.totalPage",
                records: "page.totalCount"
            },
            prmNames: {
                page: "page",
                rows: "limit",
                order: "order"
            },
            gridComplete: function () {
                //隐藏grid底部滚动条
                $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            }
        })
    );


});


function getdatadictionary(sss) {

    $.get(baseURL + "api/bdatadictionary/select?d_code=" + sss, function (r) {
        if (sss == "operation_state") {
            operation_state = r.returnlist;
        }
        if (sss == "category_type") {
            category_type = r.returnlist;
        }
        if (sss == "progect_type") {
            progect_type = r.returnlist;
        }
        vm.operation_state = operation_state;
        vm.category_type = category_type;
        vm.progect_type = progect_type;



    });
}
function showxmgk(guid) {
    var title = "项目概况";
    var content = "t3xmgk.html?guid="+guid;
    layer.open({
        type: 2,
        title: false,

        area: ['95%', '95%'],
        content: content

    });
}
function showPhotos(images) {
    parent.showPhotos(images)
}
function showcj(guid, longitude, latitude) {
    var title = "现场信息查看";
    // var content = "apbaseothorcj.html?guid=" + guid + "&longitude=" + longitude + "&latitude=" + latitude;
    var content = "dpoint212.html?guid="+guid+"&longitude="+longitude+"&latitude="+latitude;
    layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        shade: 0.5,
        maxmin: true, //开启最大化最小化按钮
        area: ['100%', '100%'],
        content: content

    });
}
function showDXF(guid, pBName) {

    var title = "走向图";

    var content ="ashowdxfandexp.html?pBName="+pBName+"&pGuid="+guid;
    layer.open({
        type: 2,
        title: false,

        area: ['95%', '95%'],
        content: content
    });
}
function showfjdc(guid) {

    var title = "导出结果";

    var content = "tjgdc.html?guid=" + guid ;
    layer.open({
        type: 2,
        title: false,

        area: ['95%', '95%'],
        content: content
    });
}
function showgzb(guid) {

    var title = "查看购置表";

    var content = "purchase.html?guid=" + guid ;
    layer.open({
        type: 2,
        title: false,

        area: ['95%', '95%'],
        content: content
    });
}
function showccb(guid) {

    var title = "查看拆除表";

    var content = "remove.html?guid=" + guid ;
    layer.open({
        type: 2,
        title: false,

        area: ['95%', '95%'],
        content: content
    });
}
function showsqs(guid) {

    var title = "查看申请书";

    var content = "cksqs.html?guid="+guid;
    layer.open({
        type: 2,
        title: false,

        area: ['95%', '95%'],
        content: content
    });
}

function getorgall() {
    $.get(baseURL + "sys/dept/selectall", function (r) {
        orgall = r.returnlist;
    });
    vm.orgall = orgall;
}
function getuserall() {
    $.get(baseURL + "sys/user/select", function (r) {
        userall = r.returnlist;

    });
    vm.userall = userall;

}

var ztreesel;
var dept_setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var vm = new Vue({
    el: '#rrapp',

    data: {
        q: {
            pBType: null,
            pBName: null,
            pBOrg: null,
            pBOrgName: null,
            pBState: null
        },

        orgall: orgall,
        userall: userall,
        userallmy:null,
        showList: true,
        showAdd: false,
        showSeach:false,
        title: null,
        aPBase: {pBCollectid: []},
        category_type: category_type,
        operation_state:operation_state,
        progect_type:progect_type,
        fw:[],
        bb:[],


    },
    methods: {
        init: function () {
            $.get(baseURL + "api/bdatadictionary/select?d_code=operation_state" , function (r) {
                operation_state = r.returnlist;
                vm.operation_state = operation_state;
            });
            $.get(baseURL + "api/bdatadictionary/select?d_code=category_type" , function (r) {
                category_type= r.returnlist;
                vm.category_type = category_type;
            });
            $.get(baseURL + "api/bdatadictionary/select?d_code=progect_type" , function (r) {
                progect_type =progect_type
                vm.progect_type = r.returnlist;
            });

            $.get(baseURL + "sys/dept/selectall", function (r) {
                ztreesel = $.fn.zTree.init($("#deptTreesel"), dept_setting, r.returnlist);
                orgall = r.returnlist;
                vm.orgall = orgall;
            });

            $.get(baseURL + "sys/user/select", function (r) {
               userall =r.returnlist;
                vm.userall = userall;
            });
            $.get(baseURL + "api/bdatadictionary/select?d_code=project_scope" , function (r) {

                vm.fw = r.returnlist;
            });
            $.get(baseURL + "api/bdatadictionary/select?d_code=edition_type" , function (r) {

                vm.bb = r.returnlist;
            });
        },
        // deptTreesel: function(){
        //     layer.open({
        //         type: 1,
        //         offset: '50px',
        //         skin: 'layui-layer-molv',
        //         title: "选择部门",
        //         area: ['300px', '450px'],
        //         shade: 0,
        //         shadeClose: false,
        //         content: jQuery("#deptLayersel"),
        //         btn: ['确定'],
        //         btn1: function (index) {
        //             vm.reload();
        //             layer.close(index);
        //
        //         }
        //     });
        // },

        deptTreesel: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择部门",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayersel"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztreesel.getSelectedNodes();

                    vm.q.pBOrg = node[0].deptId;
                    vm.q.pBOrgName = node[0].name;
                    layer.close(index);

                },
                btn2: function (index) {
                    vm.q.pBOrg ="" ;
                    vm.q.pBOrgName = "";
                    layer.close(index);

                }

            });
        },
        query: function () {
          vm.reload();
        },
        selorg:function () {
            vm.showList=false;
           vm.showAdd=false;
             vm.showSeach=true;
            vm.title = "查询";
            vm.orgall=orgall;
        },
    add: function () {

            vm.showList=false;
            vm.showAdd=true;
            vm.showSeach=false;
            vm.title = "新增";
            vm.aPBase = {pBCollectid: []},
            vm.category_type = category_type;
            vm.orgall=orgall;
            vm.userall=userall;
            vm. progect_type=progect_type;
            vm. fw=[100,500,1000];
            vm. bb=["国网","南网","地电"];
           $("#selectorg").val(orgall[0]);
            var userallmy = [];
            for(var i=0 ;i<userall.length;i++)
            {
                if(userall[i].deptId== orgall[0].deptId)
                {
                    userallmy.push(userall[i]);
                }
            }
            vm.userallmy=userallmy;
        },
        deptTree: function (){
           var ids = $("#selectorg").val();
            var userallmy = [];
            for(var i=0 ;i<userall.length;i++)
            {
                if(userall[i].deptId==ids)
                {
                    userallmy.push(userall[i]);
                }
            }
            vm.userallmy=userallmy;
        },
        update: function (event) {
            var guid = getSelectedRow();
            if (guid == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(guid);
            vm.category_type = category_type;
            vm.orgall=orgall;
            vm.userall=userall;
            vm. progect_type=progect_type;
            vm. fw=[100,500,1000];
            vm. bb=["国网","南网","地电"];



        },
        saveOrUpdate: function (event) {
            var url = vm.aPBase.guid == null ? "bss/apbase/save" : "bss/apbase/update";

            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.aPBase),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var guids = getSelectedRows();
            if (guids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "bss/apbase/delete",
                    contentType: "application/json",
                    data: JSON.stringify(guids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },

        getInfo: function (guid) {
            $.get(baseURL + "bss/apbase/info/" + guid, function (r) {
                vm.aPBase = r.aPBase;
                vm.aPBase.pBCollectid = r.aPBase.pBCollectid.replace(/\"/g, "").replace("[", "").replace("]", "").split(",");
                vm.userallmy = r.userallmy;
                var i = 0;
                var item = new Array();
                for (d in progect_type) {

                    if (progect_type[d].dKey == r.aPBase.pBType) {

                        item[i] = progect_type[d];
                        i++;
                    }
                }

            });
        },
        reload: function (event) {
                vm.showList=true;
                vm.showAdd=false;
                vm.showSeach=false;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'pBType': vm.q.pBType,
                    'pBName': vm.q.pBName,
                    'pBOrg': vm.q.pBOrg,
                    'pBState': vm.q.pBState

                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});